import socket
import os, sys
import tkinter
from tkinter import *
import tkinter.messagebox
from tkinter.font import Font
from tkinter.ttk import *
from tkinter.scrolledtext import ScrolledText  # 导入多行文本框用到的包
import json # json.dumps(some)打包   json.loads(some)解包
from docx import Document
from docx.shared import Pt


class Application_ui1(Frame):
    #生成登录界面
    def __init__(self, master=None):
        Frame.__init__(self, master)
        self.master.title('登录界面')
        self.master.geometry('402x201')
        self.createWidgets()

    def createWidgets(self):
        self.top = self.winfo_toplevel()
        self.v = StringVar()
        self.v.set(9)

        self.style = Style()

        self.Text1Var = StringVar(value='')
        self.Text1 = Entry(self.top, textvariable=self.Text1Var, font=('宋体',9))
        self.Text1.place(relx=0.239, rely=0.159, relwidth=0.46, relheight=0.124)

        self.Text2Var = StringVar(value='')
        self.Text2 = Entry(self.top, textvariable=self.Text2Var, font=('宋体',9))
        self.Text2.place(relx=0.239, rely=0.398, relwidth=0.46, relheight=0.124)

        self.style.configure('Label1.TLabel',anchor='w', foreground='#000000', font=('宋体',9))
        self.Label1 = Label(self.top, text='账号', style='Label1.TLabel')
        self.Label1.place(relx=0.139, rely=0.159, relwidth=0.077, relheight=0.1)

        self.style.configure('Label2.TLabel',anchor='w', font=('宋体',9))
        self.Label2 = Label(self.top, text='密码', style='Label2.TLabel')
        self.Label2.place(relx=0.139, rely=0.398, relwidth=0.08, relheight=0.1)

        self.style.configure('Frame1.TLabelframe',foreground='#000000',font=('宋体',9))
        self.Frame1 = LabelFrame(self.top, text='', style='Frame1.TLabelframe')
        self.Frame1.place(relx=0.139, rely=0.517, relwidth=0.679, relheight=0.403)

        self.style.configure('Option1.TRadiobutton', font=('宋体',9))
        self.Option1 = Radiobutton(self.Frame1, text='管理员登录', value=8, style='Option1.TRadiobutton',variable=self.v)
        self.Option1.place(relx=0.088, rely=0.198, relwidth=0.297, relheight=0.407)

        self.style.configure('Option2.TRadiobutton', font=('宋体',9))
        self.Option2 = Radiobutton(self.Frame1, text='用户登录', value=9, style='Option2.TRadiobutton',variable=self.v)
        self.Option2.place(relx=0.469, rely=0.198, relwidth=0.326, relheight=0.407)

        self.style.configure('Command1.TButton',font=('宋体',9))
        self.Command1 = Button(self.Frame1, text='登录', command=self.Command1_Cmd, style='Command1.TButton')
        self.Command1.place(relx=0.264, rely=0.691, relwidth=0.267, relheight=0.309)

        self.master.bind('<Return>', self.Command1_Cmd) #回车绑定登录


class Application1(Application_ui1):
    #登录界面回调函数
    def __init__(self, master=None):
        Application_ui1.__init__(self, master)

    def Command1_Cmd(self, event=None):
        user0=self.Text1.get().encode('utf-8')
        user=user0.decode()#转为str格式
        password0=self.Text2.get().encode('utf-8')
        password=password0.decode()
        
        if not user:
            tkinter.messagebox.showwarning('warning', message='用户名为空!')
        elif not password:
            tkinter.messagebox.showwarning('warning', message='密码为空!')
        else:
            data.append(0)
            data.append(user)
            data.append(password)
            data.append(self.v.get())
            
            send(data)
            l=recv()
           # print(l)
      
            if int(l)==0:
                tkinter.messagebox.showinfo("消息提示",message="登录成功")
                global admin
                if int(self.v.get())==8:
                    admin=1
                elif int(self.v.get())==9:
                    admin=0
                self.top.destroy()
            else:
                tkinter.messagebox.showinfo("消息提示",message="账号或密码错误")

        
class Application_ui2(Frame):
    #生成用户界面
    def __init__(self, master=None):
        Frame.__init__(self, master)
        self.master.title('田径运动会查询系统')
        self.master.geometry('600x350')
        self.createWidgets()

    def createWidgets(self):
        self.top = self.winfo_toplevel()
        self.v = StringVar()
        self.v.set(0)
        self.flag=0

        self.style = Style()

        self.style.configure('Command1.TButton',font=('宋体',9))
        self.Command1 = Button(self.top, text='确定', command=self.Command1_Cmd, style='Command1.TButton')
        self.Command1.place(relx=0.804, rely=0.685, relwidth=0.118, relheight=0.083)

        self.style.configure('Option5.TRadiobutton',font=('宋体',9))
        self.Option5 = Radiobutton(self.top, text='破纪录情况查询', value=5, style='Option5.TRadiobutton',variable=self.v)
        self.Option5.place(relx=0.78, rely=0.561, relwidth=0.201, relheight=0.078)

        self.style.configure('Option4.TRadiobutton',font=('宋体',9))
        self.Option4 = Radiobutton(self.top, text='队伍成绩查询', value=3, style='Option4.TRadiobutton',variable=self.v)
        self.Option4.place(relx=0.78, rely=0.312, relwidth=0.201, relheight=0.078)

        self.style.configure('Option3.TRadiobutton',font=('宋体',9))
        self.Option3 = Radiobutton(self.top, text='队伍排名查询', value=4, style='Option3.TRadiobutton',variable=self.v)
        self.Option3.place(relx=0.78, rely=0.436, relwidth=0.201, relheight=0.078)

        self.style.configure('Option2.TRadiobutton',font=('宋体',9))
        self.Option2 = Radiobutton(self.top, text='项目成绩查询', value=2, style='Option2.TRadiobutton',variable=self.v)
        self.Option2.place(relx=0.78, rely=0.187, relwidth=0.201, relheight=0.078)

        self.style.configure('Option1.TRadiobutton',font=('宋体',9))
        self.Option1 = Radiobutton(self.top, text='个人成绩查询', value=1, style='Option1.TRadiobutton',variable=self.v)
        self.Option1.place(relx=0.78, rely=0.062, relwidth=0.201, relheight=0.078)

        self.queryFont = Font(font=('宋体',9))
        self.query = Text(self.top,font=self.queryFont,state='normal')
        self.query.place(relx=0.014, rely=0.025, relwidth=0.725, relheight=0.9)
        self.query.insert('insert', '请选择你要查询的内容：')
        self.query.config(state='disabled') #锁定文本框

        self.style.configure('Command2.TButton',font=('宋体',9))
        self.Command2 = Button(self.top, text='生成报表', command=self.Command2_Cmd, style='Command2.TButton')
        self.Command2.place(relx=0.804, rely=0.831, relwidth=0.118, relheight=0.083)

        self.master.bind('<Return>', self.Command1_Cmd) #回车绑定确定


class Application2(Application_ui2):
    #用户界面回调函数
    def __init__(self, master=None):
        Application_ui2.__init__(self, master)

    def Command1_Cmd(self, event=None):
        #生成SQL查询语句       
        self.flag=1
        self.query.config(state='normal') #解除锁定状态
        if int(self.v.get())==1:
            tp=Tk()
            Application3(tp).mainloop()    
            self.query.delete("1.0","end") #清空文本框             
          #  print(condition)
            sql=(r"select * from grade where number in (select number from sportsman where number='"+condition+"' or name='"+condition+"');")
          #  print(sql)
            data.append(1)
            data.append(sql)
            send(data)
            result=recv() 
            self.query.insert('insert', '运动员号码   比赛项目   初赛成绩   决赛成绩\n')
            self.query.insert('end','___________________________________________ \n') 
            for line in result:
                self.query.insert('end',str(line[0]).ljust(13)+str(line[1]).ljust(9)+str(line[2]).ljust(11)+str(line[3])+'\n')

        if int(self.v.get())==2:
            tp=Tk()
            Application3(tp).mainloop()    
            self.query.delete("1.0","end") #清空文本框                
          #  print(condition)
            sql=(r"select * from grade where matchname='"+condition+"' order by number;")
          #  print(sql)
            data.append(1)
            data.append(sql)
            send(data)
            result=recv() 
            self.query.insert('insert', '运动员号码   比赛项目   初赛成绩   决赛成绩\n')
            self.query.insert('end','___________________________________________ \n') 
            for line in result:
                self.query.insert('end',str(line[0]).ljust(13)+str(line[1]).ljust(9)+str(line[2]).ljust(11)+str(line[3])+'\n')

        if int(self.v.get())==3:
            tp=Tk()
            Application3(tp).mainloop()    
            self.query.delete("1.0","end") #清空文本框                
          #  print(condition)
            sql=(r"select * from grade where number in(select number from sportsman where teamname='"+condition+"') order by number;")
          #  print(sql)
            data.append(1)
            data.append(sql)
            send(data)
            result=recv() 
            self.query.insert('insert', '运动员号码   比赛项目   初赛成绩   决赛成绩\n')
            self.query.insert('end','___________________________________________ \n') 
            for line in result:
                self.query.insert('end',str(line[0]).ljust(13)+str(line[1]).ljust(9)+str(line[2]).ljust(11)+str(line[3])+'\n')

        if int(self.v.get())==4: 
            self.query.delete("1.0","end") #清空文本框                
            sql=(r"select * from team order by score desc;")
            data.append(1)
            data.append(sql)
            send(data)
            result=recv() 
            self.query.insert('insert', '注：进入决赛的运动员中，冠军得5分，亚军3分，季军2分，其余得1分\n')
            self.query.insert('end','________________________________________________________________\n') 
            for line in result:
                self.query.insert('end',str(line[0]).ljust(10)+str(line[1])+'\n')
        if int(self.v.get())==5:
            self.query.delete("1.0","end") #清空文本框        
            record=[]
            sql=(r"SELECT * FROM `match` LIMIT 0, 1000;")
            data.append(1)
            data.append(sql)
            send(data)
            result=recv() 
            self.query.insert('insert', '历史记录\n')
            self.query.insert('end','___________________________________________ \n') 
            for line in result:
                self.query.insert('end',str(line[0]).ljust(10)+str(line[1])+'\n')
                record.append(float(str(line[1])[0:len(line[1])-1]))
            self.query.insert('end','\n\n本次比赛破纪录选手及成绩\n') 
            self.query.insert('end','___________________________________________ \n') 
            sql=(r"select * from grade where final != 'NULL';")
            data.clear()
            data.append(1)
            data.append(sql)
            send(data)
            result=recv() 
            for line in result:
                if line[1] == '100米跑':
                    if float(line[3][0:len(line[3])-1]) < record[0]:
                        self.query.insert('end',str(line[0]).ljust(13)+str(line[1]).ljust(9)+str(line[2]).ljust(11)+str(line[3])+'\n')
                if line[1] == '800米跑':
                    if float(line[3][0:len(line[3])-1]) < record[2]:
                        self.query.insert('end',str(line[0]).ljust(13)+str(line[1]).ljust(9)+str(line[2]).ljust(11)+str(line[3])+'\n')
                if line[1] == '3000米跑':
                    if float(line[3][0:len(line[3])-1]) < record[1]:
                        self.query.insert('end',str(line[0]).ljust(13)+str(line[1]).ljust(9)+str(line[2]).ljust(11)+str(line[3])+'\n')
                if line[1] == '跳高':
                    if float(line[3][0:len(line[3])-1]) > record[4]:
                        self.query.insert('end',str(line[0]).ljust(13)+str(line[1]).ljust(9)+str(line[2]).ljust(11)+str(line[3])+'\n')
                if line[1] == '跳远':
                    if float(line[3][0:len(line[3])-1]) > record[3]:
                        self.query.insert('end',str(line[0]).ljust(13)+str(line[1]).ljust(9)+str(line[2]).ljust(11)+str(line[3])+'\n')
        self.query.config(state='disabled') #锁定文本框



    def Command2_Cmd(self, event=None):
        #生成报表
        text=self.query.get("1.0","end")    #获取当前文本输入框的内容
        if self.flag == 0:
            tkinter.messagebox.showwarning('warning', message='无查询结果!')
        else:
            document = Document()
            document.add_paragraph(text)
            style = document.styles['Normal'] #设置字体
            font = style.font
            font.size = Pt(10)

            if int(self.v.get())==1:
                path='./报表/'+condition+'选手成绩表.docx'
                document.save(path)
            if int(self.v.get())==2:
                path='./报表/'+condition+'比赛成绩表.docx'
                document.save(path)
            if int(self.v.get())==3:
                path='./报表/'+condition+'团队成绩表.docx'
                document.save(path)
            if int(self.v.get())==4:
                path='./报表/当前队伍得分表.docx'
                document.save(path)
            if int(self.v.get())==5:
                path='./报表/破纪录情况表.docx'
                document.save(path)

class Application_ui3(Frame):
    #提示窗口
    def __init__(self, master=None):
        Frame.__init__(self, master)
        self.master.title('提示')
        self.master.geometry('304x201')
        self.createWidgets()

    def createWidgets(self):
        self.top = self.winfo_toplevel()

        self.style = Style()

        self.style.configure('Label1.TLabel',anchor='w', font=('宋体',9))
        self.Label1 = Label(self.top, text='请输入作为查询条件的关键字：', style='Label1.TLabel')
        self.Label1.place(relx=0.211, rely=0.239, relwidth=0.592, relheight=0.106)

        self.Text1Var = StringVar(value='')
        self.Text1 = Entry(self.top, textvariable=self.Text1Var, font=('宋体',9))
        self.Text1.place(relx=0.211, rely=0.358, relwidth=0.548, relheight=0.119)

        self.style.configure('Command1.TButton',font=('宋体',9))
        self.Command1 = Button(self.top, text='确定', command=self.Command1_Cmd, style='Command1.TButton')
        self.Command1.place(relx=0.395, rely=0.716, relwidth=0.214, relheight=0.124)

        self.master.bind('<Return>', self.Command1_Cmd) #回车绑定确定


class Application3(Application_ui3):
    #这个类实现具体的事件处理回调函数。界面生成代码在Application_ui中。
    def __init__(self, master=None):
        Application_ui3.__init__(self, master)
    def Command1_Cmd(self, event=None): 
        te = self.Text1.get().encode('utf-8')
        global condition
        condition = te.decode()
        if not condition:
            tkinter.messagebox.showwarning('warning', message='输入为空!')
        else:
           # print(condition)
            self.top.quit()
            self.top.destroy()
        
class Application_ui4(Frame):
    #管理界面生成
    def __init__(self, master=None):
        Frame.__init__(self, master)
        self.master.title('田径运动会管理系统')
        self.master.geometry('600x300')
        self.createWidgets()

    def createWidgets(self):
        self.top = self.winfo_toplevel()

        self.style = Style()
        self.v1 = StringVar()
        self.v1.set(0)
        self.v2 = StringVar()
        self.v2.set(0)

        self.style.configure('Label1.TLabel',anchor='w', font=('宋体',9))
        self.Label1 = Label(self.top, text='操作', style='Label1.TLabel')
        self.Label1.place(relx=0.044, rely=0.13, relwidth=0.074, relheight=0.172)

        self.style.configure('Option1.TRadiobutton',font=('宋体',9))
        self.Option1 = Radiobutton(self.top, text='添加', value=1, style='Option1.TRadiobutton',variable=self.v1)
        self.Option1.place(relx=0.133, rely=0.13, relwidth=0.118, relheight=0.172)

        self.style.configure('Option2.TRadiobutton',font=('宋体',9))
        self.Option2 = Radiobutton(self.top, text='修改', value=2, style='Option2.TRadiobutton',variable=self.v1)
        self.Option2.place(relx=0.266, rely=0.13, relwidth=0.118, relheight=0.172)

        self.style.configure('Option3.TRadiobutton',font=('宋体',9))
        self.Option3 = Radiobutton(self.top, text='删除', value=3, style='Option3.TRadiobutton',variable=self.v1)
        self.Option3.place(relx=0.399, rely=0.13, relwidth=0.118, relheight=0.172)

        self.style.configure('Label2.TLabel',anchor='w', font=('宋体',9))
        self.Label2 = Label(self.top, text='表', style='Label2.TLabel')
        self.Label2.place(relx=0.044, rely=0.324, relwidth=0.074, relheight=0.172)

        self.style.configure('Option5.TRadiobutton',font=('宋体',9))
        self.Option5 = Radiobutton(self.top, text='user', value=1, style='Option5.TRadiobutton',variable=self.v2)
        self.Option5.place(relx=0.133, rely=0.324, relwidth=0.118, relheight=0.172)

        self.style.configure('Option6.TRadiobutton',font=('宋体',9))
        self.Option6 = Radiobutton(self.top, text='match', value=2, style='Option6.TRadiobutton',variable=self.v2)
        self.Option6.place(relx=0.266, rely=0.324, relwidth=0.118, relheight=0.172)

        self.style.configure('Option7.TRadiobutton',font=('宋体',9))
        self.Option7 = Radiobutton(self.top, text='sportsman', value=3, style='Option7.TRadiobutton',variable=self.v2)
        self.Option7.place(relx=0.399, rely=0.324, relwidth=0.177, relheight=0.172)

        self.style.configure('Option8.TRadiobutton',font=('宋体',9))
        self.Option8 = Radiobutton(self.top, text='team', value=4, style='Option8.TRadiobutton',variable=self.v2)
        self.Option8.place(relx=0.577, rely=0.324, relwidth=0.118, relheight=0.172)

        self.style.configure('Option9.TRadiobutton',font=('宋体',9))
        self.Option9 = Radiobutton(self.top, text='grade', value=5, style='Option9.TRadiobutton',variable=self.v2)
        self.Option9.place(relx=0.71, rely=0.324, relwidth=0.118, relheight=0.172)

        self.style.configure('Command1.TButton',font=('宋体',9))
        self.Command1 = Button(self.top, text='确定', command=self.Command1_Cmd, style='Command1.TButton')
        self.Command1.place(relx=0.399, rely=0.649, relwidth=0.148, relheight=0.18)
        self.master.bind('<Return>', self.Command1_Cmd) #回车绑定确定


class Application4(Application_ui4):
    #管理界面回调函数
    def __init__(self, master=None):
        Application_ui4.__init__(self, master)

    def Command1_Cmd(self, event=None):
        global key
        key=int(self.v1.get())
        if int(self.v1.get())==1: #添加
            if int(self.v2.get())==1: #user表
                tp=Tk()
                Application5(tp).mainloop()
                tkinter.messagebox.showinfo("消息提示",message="添加成功！")
            if int(self.v2.get())==2: #match表
                tp=Tk()
                Application7(tp).mainloop()
                tkinter.messagebox.showinfo("消息提示",message="添加成功！")
            if int(self.v2.get())==4: #team表
                tp=Tk()
                Application8(tp).mainloop()
                tkinter.messagebox.showinfo("消息提示",message="添加成功！")
            if int(self.v2.get())==3: #sportsman表
                tp=Tk()
                Application9(tp).mainloop()
                tkinter.messagebox.showinfo("消息提示",message="添加成功！")
            if int(self.v2.get())==5: #grade表
                tp=Tk()
                Application10(tp).mainloop()
                tkinter.messagebox.showinfo("消息提示",message="添加成功！")

        elif int(self.v1.get())==2: #修改
            if int(self.v2.get())==1: #user表
                tp=Tk()
                Application3(tp).mainloop()
                tp=Tk()
                Application6(tp).mainloop()
                tkinter.messagebox.showinfo("消息提示",message="修改成功！")
            if int(self.v2.get())==2: #match表
                tp=Tk()
                Application3(tp).mainloop()
                tp=Tk()
                Application7(tp).mainloop()
                tkinter.messagebox.showinfo("消息提示",message="修改成功！")
            if int(self.v2.get())==4: #team表
                tp=Tk()
                Application3(tp).mainloop()
                tp=Tk()
                Application8(tp).mainloop()
                tkinter.messagebox.showinfo("消息提示",message="修改成功！")
            if int(self.v2.get())==3: #sportsman表
                tp=Tk()
                Application3(tp).mainloop()
                tp=Tk()
                Application9(tp).mainloop()
                tkinter.messagebox.showinfo("消息提示",message="修改成功！")
            if int(self.v2.get())==5: #grade表
                tp=Tk()
                Application10(tp).mainloop()
                tkinter.messagebox.showinfo("消息提示",message="修改成功！")

        elif int(self.v1.get())==3: #删除
            if int(self.v2.get())==1: #user表
                tp=Tk()
                Application3(tp).mainloop()
                sql=(r"delete from user where username='"+condition+"';")
                sql=(r"delete from admin where username='"+condition+"';")
                data.append(2)
                data.append(sql)
                send(data)
                tkinter.messagebox.showinfo("消息提示",message="删除成功！")
            if int(self.v2.get())==2: #match表
                tp=Tk()
                Application3(tp).mainloop()
                sql=(r"delete from `match` where matchname='"+condition+"';")
                data.append(2)
                data.append(sql)
                send(data)
                tkinter.messagebox.showinfo("消息提示",message="删除成功！")
            if int(self.v2.get())==4: #team表
                tp=Tk()
                Application3(tp).mainloop()
                sql=(r"delete from `team` where teamname='"+condition+"';")
                data.append(2)
                data.append(sql)
                send(data)
                tkinter.messagebox.showinfo("消息提示",message="删除成功！")
            if int(self.v2.get())==3: #sportsman表
                tp=Tk()
                Application3(tp).mainloop()
                sql=(r"delete from `sportsman` where number='"+condition+"';")
                data.append(2)
                data.append(sql)
                send(data)
                tkinter.messagebox.showinfo("消息提示",message="删除成功！")
            if int(self.v2.get())==5: #grade表
                tp=Tk()
                Application11(tp).mainloop()
                tkinter.messagebox.showinfo("消息提示",message="删除成功！")


#添加用户界面
class Application_ui5(Frame):
    def __init__(self, master=None):
        Frame.__init__(self, master)
        self.master.title('from')
        self.master.geometry('500x300')
        self.createWidgets()

    def createWidgets(self):
        self.top = self.winfo_toplevel()

        self.style = Style()
        self.v = StringVar(value=2)

        self.style.configure('Option1.TRadiobutton',font=('宋体',9))
        self.Option1 = Radiobutton(self.top, text='是', value=1, style='Option1.TRadiobutton',variable=self.v)
        self.Option1.place(relx=0.521, rely=0.565, relwidth=0.153, relheight=0.15)

        self.style.configure('Command1.TButton',font=('宋体',9))
        self.Command1 = Button(self.top, text='确定', command=self.Command1_Cmd, style='Command1.TButton')
        self.Command1.place(relx=0.26, rely=0.706, relwidth=0.414, relheight=0.147)

        self.Text2Var = StringVar(value='')
        self.Text2 = Entry(self.top, textvariable=self.Text2Var, font=('宋体',9))
        self.Text2.place(relx=0.335, rely=0.376, relwidth=0.558, relheight=0.118)

        self.Text1Var = StringVar(value='')
        self.Text1 = Entry(self.top, textvariable=self.Text1Var, font=('宋体',9))
        self.Text1.place(relx=0.335, rely=0.141, relwidth=0.558, relheight=0.118)

        self.style.configure('Label2.TLabel',anchor='w', font=('宋体',9))
        self.Label2 = Label(self.top, text='password', style='Label2.TLabel')
        self.Label2.place(relx=0.074, rely=0.376, relwidth=0.223, relheight=0.171)

        self.style.configure('Label1.TLabel',anchor='w', font=('宋体',9))
        self.Label1 = Label(self.top, text='username', style='Label1.TLabel')
        self.Label1.place(relx=0.074, rely=0.141, relwidth=0.223, relheight=0.171)

        self.style.configure('Label3.TLabel',anchor='w', font=('宋体',9))
        self.Label3 = Label(self.top, text='是否设为管理员：', style='Label3.TLabel')
        self.Label3.place(relx=0.037, rely=0.565, relwidth=0.447, relheight=0.171)

        self.style.configure('Option2.TRadiobutton',font=('宋体',9))
        self.Option2 = Radiobutton(self.top, text='否', value=2, style='Option2.TRadiobutton',variable=self.v)
        self.Option2.place(relx=0.744, rely=0.565, relwidth=0.191, relheight=0.15)
        self.master.bind('<Return>', self.Command1_Cmd) #回车绑定确定


class Application5(Application_ui5):
    def __init__(self, master=None):
        Application_ui5.__init__(self, master)

    def Command1_Cmd(self, event=None):
        if not self.Text1.get() or not self.Text2.get():
            tkinter.messagebox.showinfo("消息提示",message="有空项！！！")
        if int(self.v.get()) == 1:
            sql=(r"insert into admin values('"+str(self.Text1.get())+"','"+str(self.Text2.get())+"');")
            data.append(2)
            data.append(sql)
            send(data)
            sql=(r"insert into user values('"+str(self.Text1.get())+"','"+str(self.Text2.get())+"');")
            data.append(2)
            data.append(sql)
            send(data)
        elif int(self.v.get()) == 2:
            sql=(r"insert into user values('"+str(self.Text1.get())+"','"+str(self.Text2.get())+"');")
            data.append(2)
            data.append(sql)
            send(data)
        self.top.quit()
        self.top.destroy()


#修改用户界面
class Application_ui6(Frame):
    def __init__(self, master=None):
        Frame.__init__(self, master)
        self.master.title('from')
        self.master.geometry('500x300')
        self.createWidgets()

    def createWidgets(self):
        self.top = self.winfo_toplevel()

        self.style = Style()

        self.style.configure('Command1.TButton',font=('宋体',9))
        self.Command1 = Button(self.top, text='确定', command=self.Command1_Cmd, style='Command1.TButton')
        self.Command1.place(relx=0.26, rely=0.706, relwidth=0.414, relheight=0.147)

        self.Text2Var = StringVar(value='')
        self.Text2 = Entry(self.top, textvariable=self.Text2Var, font=('宋体',9))
        self.Text2.place(relx=0.335, rely=0.376, relwidth=0.558, relheight=0.118)

        self.Text1Var = StringVar(value='')
        self.Text1 = Entry(self.top, textvariable=self.Text1Var, font=('宋体',9))
        self.Text1.place(relx=0.335, rely=0.141, relwidth=0.558, relheight=0.118)

        self.style.configure('Label2.TLabel',anchor='w', font=('宋体',9))
        self.Label2 = Label(self.top, text='password', style='Label2.TLabel')
        self.Label2.place(relx=0.074, rely=0.376, relwidth=0.223, relheight=0.171)

        self.style.configure('Label1.TLabel',anchor='w', font=('宋体',9))
        self.Label1 = Label(self.top, text='username', style='Label1.TLabel')
        self.Label1.place(relx=0.074, rely=0.141, relwidth=0.223, relheight=0.171)
        self.master.bind('<Return>', self.Command1_Cmd) #回车绑定确定


class Application6(Application_ui6):
    def __init__(self, master=None):
        Application_ui6.__init__(self, master)

    def Command1_Cmd(self, event=None):
        if not self.Text1.get() or not self.Text2.get():
            tkinter.messagebox.showinfo("消息提示",message="有空项！！！")
        else:
            sql=(r"update user set username='"+str(self.Text1.get())+"',password='"+str(self.Text2.get())+"' where username='"+condition+"';")
            data.append(2)
            data.append(sql)
            send(data)
            sql=(r"update admin set username='"+str(self.Text1.get())+"',password='"+str(self.Text2.get())+"' where username='"+condition+"';")
            data.append(2)
            data.append(sql)
            send(data)
            self.top.quit()
            self.top.destroy()
  

   #match添加     
class Application_ui7(Frame):
  
    def __init__(self, master=None):
        Frame.__init__(self, master)
        self.master.title('form')
        self.master.geometry('500x300')
        self.createWidgets()

    def createWidgets(self):
        self.top = self.winfo_toplevel()

        self.style = Style()

        self.style.configure('Command1.TButton',font=('宋体',9))
        self.Command1 = Button(self.top, text='确定', command=self.Command1_Cmd, style='Command1.TButton')
        self.Command1.place(relx=0.26, rely=0.706, relwidth=0.414, relheight=0.147)

        self.Text2Var = StringVar(value='')
        self.Text2 = Entry(self.top, textvariable=self.Text2Var, font=('宋体',9))
        self.Text2.place(relx=0.335, rely=0.376, relwidth=0.558, relheight=0.118)

        self.Text1Var = StringVar(value='')
        self.Text1 = Entry(self.top, textvariable=self.Text1Var, font=('宋体',9))
        self.Text1.place(relx=0.335, rely=0.141, relwidth=0.558, relheight=0.118)

        self.style.configure('Label2.TLabel',anchor='w', font=('宋体',9))
        self.Label2 = Label(self.top, text='record', style='Label2.TLabel')
        self.Label2.place(relx=0.074, rely=0.376, relwidth=0.223, relheight=0.171)

        self.style.configure('Label1.TLabel',anchor='w', font=('宋体',9))
        self.Label1 = Label(self.top, text='matchname', style='Label1.TLabel')
        self.Label1.place(relx=0.074, rely=0.141, relwidth=0.223, relheight=0.171)
        self.master.bind('<Return>', self.Command1_Cmd) #回车绑定确定


class Application7(Application_ui7):
    def __init__(self, master=None):
        Application_ui7.__init__(self, master)

    def Command1_Cmd(self, event=None):
        if not self.Text1.get() or not self.Text2.get():
            tkinter.messagebox.showinfo("消息提示",message="有空项！！！")
        else:
            if key == 1:
                sql=(r"insert into `match` values('"+str(self.Text1.get())+"','"+str(self.Text2.get())+"');")
            elif key == 2:
                sql=(r"update `match` set matchname='"+str(self.Text1.get())+"',record='"+str(self.Text2.get())+"' where matchname='"+condition+"';")
            data.append(2)
            data.append(sql)
            send(data)    
            self.top.quit()
            self.top.destroy()

#team
class Application_ui8(Frame):
    def __init__(self, master=None):
        Frame.__init__(self, master)
        self.master.title('from')
        self.master.geometry('500x300')
        self.createWidgets()

    def createWidgets(self):
        self.top = self.winfo_toplevel()

        self.style = Style()

        self.style.configure('Command1.TButton',font=('宋体',9))
        self.Command1 = Button(self.top, text='确定', command=self.Command1_Cmd, style='Command1.TButton')
        self.Command1.place(relx=0.26, rely=0.706, relwidth=0.414, relheight=0.147)

        self.Text2Var = StringVar(value='')
        self.Text2 = Entry(self.top, textvariable=self.Text2Var, font=('宋体',9))
        self.Text2.place(relx=0.335, rely=0.376, relwidth=0.558, relheight=0.118)

        self.Text1Var = StringVar(value='')
        self.Text1 = Entry(self.top, textvariable=self.Text1Var, font=('宋体',9))
        self.Text1.place(relx=0.335, rely=0.141, relwidth=0.558, relheight=0.118)

        self.style.configure('Label2.TLabel',anchor='w', font=('宋体',9))
        self.Label2 = Label(self.top, text='score', style='Label2.TLabel')
        self.Label2.place(relx=0.074, rely=0.376, relwidth=0.223, relheight=0.171)

        self.style.configure('Label1.TLabel',anchor='w', font=('宋体',9))
        self.Label1 = Label(self.top, text='teamname', style='Label1.TLabel')
        self.Label1.place(relx=0.074, rely=0.141, relwidth=0.223, relheight=0.171)
        self.master.bind('<Return>', self.Command1_Cmd) #回车绑定确定


class Application8(Application_ui8):
    def __init__(self, master=None):
        Application_ui8.__init__(self, master)

    def Command1_Cmd(self, event=None):
        if not self.Text1.get() or not self.Text2.get():
            tkinter.messagebox.showinfo("消息提示",message="有空项！！！")
        else:
            if key == 1:
                sql=(r"insert into `team` values('"+str(self.Text1.get())+"','"+str(self.Text2.get())+"');")
            elif key == 2:
                sql=(r"update `team` set teamname='"+str(self.Text1.get())+"',score='"+str(self.Text2.get())+"' where teamname='"+condition+"';")
            data.append(2)
            data.append(sql)
            send(data)    
            self.top.quit()
            self.top.destroy()

#sportsman
class Application_ui9(Frame):
    def __init__(self, master=None):
        Frame.__init__(self, master)
        self.master.title('from')
        self.master.geometry('500x300')
        self.createWidgets()

    def createWidgets(self):
        self.top = self.winfo_toplevel()

        self.style = Style()

        self.style.configure('Label1.TLabel',anchor='w', font=('宋体',9))
        self.Label1 = Label(self.top, text='number', style='Label1.TLabel')
        self.Label1.place(relx=0.068, rely=0.104, relwidth=0.153, relheight=0.162)

        self.style.configure('Label2.TLabel',anchor='w', font=('宋体',9))
        self.Label2 = Label(self.top, text='name', style='Label2.TLabel')
        self.Label2.place(relx=0.068, rely=0.311, relwidth=0.102, relheight=0.162)

        self.style.configure('Label3.TLabel',anchor='w', font=('宋体',9))
        self.Label3 = Label(self.top, text='teamname', style='Label3.TLabel')
        self.Label3.place(relx=0.068, rely=0.518, relwidth=0.204, relheight=0.162)

        self.Text1Var = StringVar(value='')
        self.Text1 = Entry(self.top, textvariable=self.Text1Var, font=('宋体',9))
        self.Text1.place(relx=0.34, rely=0.104, relwidth=0.511, relheight=0.104)

        self.Text2Var = StringVar(value='')
        self.Text2 = Entry(self.top, textvariable=self.Text2Var, font=('宋体',9))
        self.Text2.place(relx=0.34, rely=0.311, relwidth=0.511, relheight=0.104)

        self.Text3Var = StringVar(value='')
        self.Text3 = Entry(self.top, textvariable=self.Text3Var, font=('宋体',9))
        self.Text3.place(relx=0.34, rely=0.518, relwidth=0.511, relheight=0.104)

        self.style.configure('Command1.TButton',font=('宋体',9))
        self.Command1 = Button(self.top, text='确定', command=self.Command1_Cmd, style='Command1.TButton')
        self.Command1.place(relx=0.272, rely=0.746, relwidth=0.345, relheight=0.171)
        self.master.bind('<Return>', self.Command1_Cmd) #回车绑定确定

class Application9(Application_ui9):
    def __init__(self, master=None):
        Application_ui9.__init__(self, master)

    def Command1_Cmd(self, event=None):
        if not self.Text1.get() or not self.Text2.get():
            tkinter.messagebox.showinfo("消息提示",message="有空项！！！")
        else:
            if key == 1:
                sql=(r"insert into `sportsman` values('"+str(self.Text1.get())+"','"+str(self.Text2.get())+"','"+str(self.Text3.get())+"');")
            elif key == 2:
                sql=(r"update `sportsman` set number='"+str(self.Text1.get())+"',name='"+str(self.Text2.get())+"',teamname='"+str(self.Text3.get())+"' where number='"+condition+"';")
            data.append(2)
            data.append(sql)
            send(data)    
            self.top.quit()
            self.top.destroy()

#grade
class Application_ui10(Frame):
    #这个类仅实现界面生成功能，具体事件处理代码在子类Application中。
    def __init__(self, master=None):
        Frame.__init__(self, master)
        self.master.title('from')
        self.master.geometry('500x300')
        self.createWidgets()

    def createWidgets(self):
        self.top = self.winfo_toplevel()

        self.style = Style()

        self.style.configure('Label1.TLabel',anchor='w', font=('宋体',9))
        self.Label1 = Label(self.top, text='number', style='Label1.TLabel')
        self.Label1.place(relx=0.074, rely=0.082, relwidth=0.266, relheight=0.149)

        self.style.configure('Label2.TLabel',anchor='w', font=('宋体',9))
        self.Label2 = Label(self.top, text='matchname', style='Label2.TLabel')
        self.Label2.place(relx=0.074, rely=0.247, relwidth=0.349, relheight=0.149)

        self.style.configure('Label3.TLabel',anchor='w', font=('宋体',9))
        self.Label3 = Label(self.top, text='grade', style='Label3.TLabel')
        self.Label3.place(relx=0.074, rely=0.412, relwidth=0.238, relheight=0.149)

        self.Text1Var = StringVar(value='')
        self.Text1 = Entry(self.top, textvariable=self.Text1Var, font=('宋体',9))
        self.Text1.place(relx=0.369, rely=0.082, relwidth=0.553, relheight=0.082)

        self.Text2Var = StringVar(value='')
        self.Text2 = Entry(self.top, textvariable=self.Text2Var, font=('宋体',9))
        self.Text2.place(relx=0.369, rely=0.247, relwidth=0.553, relheight=0.082)

        self.Text3Var = StringVar(value='')
        self.Text3 = Entry(self.top, textvariable=self.Text3Var, font=('宋体',9))
        self.Text3.place(relx=0.369, rely=0.412, relwidth=0.553, relheight=0.082)

        self.style.configure('Command1.TButton',font=('宋体',9))
        self.Command1 = Button(self.top, text='确定', command=self.Command1_Cmd, style='Command1.TButton')
        self.Command1.place(relx=0.258, rely=0.757, relwidth=0.373, relheight=0.136)

        self.style.configure('Label4.TLabel',anchor='w', font=('宋体',9))
        self.Label4 = Label(self.top, text='final', style='Label4.TLabel')
        self.Label4.place(relx=0.074, rely=0.576, relwidth=0.238, relheight=0.149)

        self.Text4Var = StringVar(value='')
        self.Text4 = Entry(self.top, textvariable=self.Text4Var, font=('宋体',9))
        self.Text4.place(relx=0.369, rely=0.576, relwidth=0.553, relheight=0.082)
        self.master.bind('<Return>', self.Command1_Cmd) #回车绑定确定

class Application10(Application_ui10):
    def __init__(self, master=None):
        Application_ui10.__init__(self, master)

    def Command1_Cmd(self, event=None):
        if not self.Text1.get() or not self.Text2.get():
            tkinter.messagebox.showinfo("消息提示",message="有空项！！！")
        else:
            if key == 1:
                sql=(r"insert into `grade` values('"+str(self.Text1.get())+"','"+str(self.Text2.get())+"','"+str(self.Text3.get())+"','"+str(self.Text4.get())+"');")
            elif key == 2:
                sql=(r"update `grade` set number='"+str(self.Text1.get())+"',matchname='"+str(self.Text2.get())+"',grade='"+str(self.Text3.get())+"',final='"+str(self.Text4.get())+"' where number='"+str(self.Text1.get())+"' and matchname='"+str(self.Text2.get())+"';")
            data.append(2)
            data.append(sql)
            send(data)    
            self.top.quit()
            self.top.destroy()

#grade删除     
class Application_ui11(Frame):
  
    def __init__(self, master=None):
        Frame.__init__(self, master)
        self.master.title('form')
        self.master.geometry('500x300')
        self.createWidgets()

    def createWidgets(self):
        self.top = self.winfo_toplevel()

        self.style = Style()

        self.style.configure('Command1.TButton',font=('宋体',9))
        self.Command1 = Button(self.top, text='确定', command=self.Command1_Cmd, style='Command1.TButton')
        self.Command1.place(relx=0.26, rely=0.706, relwidth=0.414, relheight=0.147)

        self.Text2Var = StringVar(value='')
        self.Text2 = Entry(self.top, textvariable=self.Text2Var, font=('宋体',9))
        self.Text2.place(relx=0.335, rely=0.376, relwidth=0.558, relheight=0.118)

        self.Text1Var = StringVar(value='')
        self.Text1 = Entry(self.top, textvariable=self.Text1Var, font=('宋体',9))
        self.Text1.place(relx=0.335, rely=0.141, relwidth=0.558, relheight=0.118)

        self.style.configure('Label2.TLabel',anchor='w', font=('宋体',9))
        self.Label2 = Label(self.top, text='matchname', style='Label2.TLabel')
        self.Label2.place(relx=0.074, rely=0.376, relwidth=0.223, relheight=0.171)

        self.style.configure('Label1.TLabel',anchor='w', font=('宋体',9))
        self.Label1 = Label(self.top, text='number', style='Label1.TLabel')
        self.Label1.place(relx=0.074, rely=0.141, relwidth=0.223, relheight=0.171)
        self.master.bind('<Return>', self.Command1_Cmd) #回车绑定确定


class Application11(Application_ui11):
    def __init__(self, master=None):
        Application_ui11.__init__(self, master)

    def Command1_Cmd(self, event=None):
        if not self.Text1.get() or not self.Text2.get():
            tkinter.messagebox.showinfo("消息提示",message="有空项！！！")
        else:
            sql=(r"delete from `grade` where number='"+str(self.Text1.get())+"' and matchname='"+str(self.Text2.get())+"';")
            data.append(2)
            data.append(sql)
            send(data)    
            self.top.quit()
            self.top.destroy()




def send(data):
    info = json.dumps(data)
    client.send(bytes(info.encode('utf-8')))
    data.clear()

def recv():
    a = client.recv(102400)
   # print(a)
    if isinstance (a,str):
        return a
    json_string = json.loads(a, strict=False)
  #  print(json_string)
    return json_string



if __name__ == "__main__":
    global client,admin,condition,data,key #全局变量
    client = socket.socket() 
    data=[]
    condition=''
    admin=2 #1为管理员登录，0为用户登录
    key=-1

    client.connect(('127.0.0.1',8888))
    top = Tk()
    Application1(top).mainloop()
    top = Tk()
    if admin==1:
        Application4(top).mainloop()
    elif admin==0:
        Application2(top).mainloop()

    


    
